﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore.Models;
using MVCQuick.Framework.Repository;

namespace MvcMusicStore.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class StoreManagerController : Controller
    {
        private IRepository repository;

        public IRepository Repository
        {
            get
            {
                return repository;
            }
            set
            {
                repository = value;
            }
        }

        //
        // GET: /StoreManager/

        public ViewResult Index()
        {
            var albums = repository.Query<Album>().ToList();
            return View(albums.ToList());
        }

        //
        // GET: /StoreManager/Details/5

        public ViewResult Details(int id)
        {
            Album album = repository.Get<Album>(id);
            return View(album);
        }

        //
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(repository.Query<Genre>().ToList(), "Id", "Name");
            ViewBag.ArtistId = new SelectList(repository.Query<Artist>().ToList(), "Id", "Name");
            return View();
        } 

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(AlbumEditModel album)
        {
            if (ModelState.IsValid)
            {
                AlbumEditModel.Create(repository, album);
                
                return RedirectToAction("Index");  
            }

            ViewBag.GenreId = new SelectList(repository.Query<Genre>().ToList(), "Id", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(repository.Query<Artist>().ToList(), "Id", "Name", album.ArtistId);
            return View(album);
        }
        
        //
        // GET: /StoreManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            AlbumEditModel album = AlbumEditModel.Get(repository, id);

            ViewBag.GenreId = new SelectList(repository.Query<Genre>().ToList(), "Id", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(repository.Query<Artist>().ToList(), "Id", "Name", album.ArtistId);
            return View(album);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(AlbumEditModel album)
        {
            if (ModelState.IsValid)
            {
                AlbumEditModel.Update(repository, album);

                return RedirectToAction("Index");
            }
            ViewBag.GenreId = new SelectList(repository.Query<Genre>().ToList(), "Id", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(repository.Query<Artist>().ToList(), "Id", "Name", album.ArtistId);
            return View(album);
        }

        //
        // GET: /StoreManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            AlbumEditModel album = AlbumEditModel.Get(repository, id);

            return View(album);
        }

        //
        // POST: /StoreManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            AlbumEditModel.Delete(repository, id);

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {           
            base.Dispose(disposing);
        }
    }
}